"""
社交互动管理后台
"""
from django.contrib import admin
from .models import Post, PostImage, Comment, Like


class PostImageInline(admin.TabularInline):
    """动态图片内联管理"""
    model = PostImage
    extra = 1


class CommentInline(admin.TabularInline):
    """评论内联管理"""
    model = Comment
    extra = 0
    readonly_fields = ('created_at', 'updated_at')


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('id', 'user', 'content_preview', 'route_plan', 'created_at', 'likes_count', 'comments_count')
    list_filter = ('created_at',)
    search_fields = ('user__username', 'content')
    ordering = ('-created_at',)
    inlines = [PostImageInline, CommentInline]
    readonly_fields = ('created_at', 'updated_at')
    
    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = '内容预览'
    
    def likes_count(self, obj):
        return obj.likes.count()
    likes_count.short_description = '点赞数'
    
    def comments_count(self, obj):
        return obj.comments.count()
    comments_count.short_description = '评论数'


@admin.register(PostImage)
class PostImageAdmin(admin.ModelAdmin):
    list_display = ('post', 'image', 'created_at')
    list_filter = ('created_at',)
    search_fields = ('post__user__username',)
    ordering = ('-created_at',)


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('user', 'post', 'content_preview', 'created_at')
    list_filter = ('created_at',)
    search_fields = ('user__username', 'content', 'post__user__username')
    ordering = ('-created_at',)
    readonly_fields = ('created_at', 'updated_at')
    
    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = '评论内容'


@admin.register(Like)
class LikeAdmin(admin.ModelAdmin):
    list_display = ('user', 'post', 'created_at')
    list_filter = ('created_at',)
    search_fields = ('user__username', 'post__user__username')
    ordering = ('-created_at',)


